<template>
  <div>
    <!--商品详情-->
    <Main_header></Main_header>
    <div class="w store-content">
      <div class="gray-box">
        <div class="gallery-wrapper">
          <div class="gallery">
            <div class="thumb">
              <div class="big">
                <img :src="good.imgurl" :alt="good.goodName">
              </div>
            </div>
          </div>
        </div>
        <!--右边-->
        <div class="banner">
          <div class="sku-custom-title">
            <el-button type="text" v-if="storeInfo" @click="toStore">{{username}}{{storeInfo}}</el-button>
            <h4>{{good.goodName}}</h4>
            <h6>
              <span>商品描述:{{good.description}}</span>
              <span class="price">
              <em>¥</em><i>{{good.rentPriceHour}}/小时</i></span>
            </h6>
            <h6>
              <el-tag size="small">{{good.goodNewold}}/成新</el-tag>
              <span class="price">
              <em>¥</em><i>{{good.rentPriceDay}}/天</i></span>
            </h6>
            <h6>
              <span>出租方用户:{{username}}</span>
              <span class="price">
              <em>押金¥</em><i>{{good.yaPrice}}</i></span>
            </h6>
          </div>
          <div class="num">
            <span class="params-name">使用须知</span>
            <div>{{good.usedKnow}}</div>
          </div>
          <div class="buy">
            <el-button type="primary" @click="addCheckout(false)">添加订单</el-button>
            <el-button type="success" @click="checkout(true)">现在租赁</el-button>
          </div>
        </div>
      </div>
      <!--商品评价展示-->
      <div class="contentImg" style="width: 86%;margin: 10px auto">
        <div class="title">
          <h4><a>商品评价  >>  </a></h4>
        </div>
        <el-empty v-if="kong" description="什么评论都没有..."></el-empty>
      </div>
      <div v-if="kapian">

        <div class="main">
          <div class="item" v-for="(item,i) in comment" :key="i">
            <div class="comment">
              <div class="user-popover-box">
                <el-avatar class="header-img" :src="item.avatar"></el-avatar>
              </div>
              <div class="content-box">
                <div class="meta-box">
                  <span class="username">{{item.nickname}}</span>
                </div>
                <div class="content">{{item.review}}</div>
                <div class="reply-stat">
                  <span class="author-time">{{item.create_time}}</span>
                  <div class="icon-btn">
              <span>
                <!--<i class="iconfont el-icon-s-comment"></i>-->
                  <el-rate v-model="item.star" disabled  show-score>商品相符:</el-rate>
                <!--商品相符({{item.star}})-->
              </span>
                    <span>
                <i class="iconfont el-icon-thumb"></i>
                服务态度({{item.manner}})
              </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <Main_footer style="margin:0 auto"></Main_footer>
    <!--弹框-->
    <el-dialog title="完善租赁信息" :visible.sync="dialogVisible"
               style="width: 1100px;margin: auto">
      <!--监听数量-->
      <div>
        <span class="demonstration" style="margin-right: 50px">数量</span>
        <el-input-number style="margin-bottom: 20px" v-model="number" :min="1" :max="100" label="描述文字"></el-input-number>
      </div>
      <!--监听计费规则-->
      <span class="demonstration" style="margin-right: 50px">计费选择</span>
      <el-radio-group v-model="time" size="medium" style="margin-bottom: 20px">
        <el-radio value="0" label=0>{{good.rentPriceHour}}/小时</el-radio>
        <el-radio value="1" label=1>{{good.rentPriceDay}}/天</el-radio>
      </el-radio-group>
      <!--监听租赁时间段-->
      <div class="block">
        <span class="demonstration" style="margin-right: 20px">租赁时间</span>
        <el-date-picker
          v-model="rentTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          >
        </el-date-picker>
      </div>
      <div v-if="hourCount">
        <span style="color: #b7c1b9;margin-left: 80px;font-size: 12px">共计{{hourCount}}小时</span>
      </div>
      <div v-if="dayCount">
        <span style="color: #b7c1b9;margin-left: 80px;font-size: 12px">共计{{dayCount}}天</span>
      </div>
      <h6>
        <span class="minPrice">
          <em>¥</em><i style="padding-right: 30px">押金:{{good.yaPrice}}</i>
          <em>¥</em><i style="padding-right: 40px">租赁价格:{{yatolPrice}}</i></span>
      </h6>
      <h6>
        <span class="price">
          <em>¥</em><i style="padding-right: 40px">总价:{{rent.tolRentPrice}}</i></span>
      </h6>
      <span slot="footer" style="padding-right: 40px">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRent">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import Main_header from '../../components/main/Main_header'
  import Main_footer from '../../components/main/Main_footer'
  export default {
    name: 'Detail',
    data () {
      return {
        user:JSON.parse(window.sessionStorage.getItem('user')),
        ispay:undefined,
        good: {},
        username:'',
        yatolPrice:'',
        rent:{
          yarent:undefined,
          myUserId:undefined,
          userId:undefined,
          goodId:undefined,
          number:undefined,
          singlePrice:undefined,
          dayPrice:undefined,
          tolRentPrice:undefined,
          paystate:'0',
          rentState:'0',
          orderAddress:undefined,
          starttime:'',
          endtime:'',
          rentTime:'',
        },
        rentTime:'',
        time:'0',
        number:1,
        rentPriceHour:'',
        rentPriceDay:'',
        dialogVisible: false,
        hourCount:'',
        dayCount:'',
        comment:[],
        kong:false,
        kapian:false,
        replys:[],
        storeInfo:'',
      }
    },
    mounted(){
      this.initDetails();
      this.initComment();
      // 用户id
      let userId = parseInt(this.user.userId);
      this.rent.userId=userId;
    },
    computed:{
      // 监控事件
      allChange(){
        const {number,time,rentTime}=this;
        return{
          number,time,rentTime
        }
      }
    },
    watch:{
      allChange(e){
        // 获取租赁时间段
        this.rent.number=e.number
        this.rent.starttime=this.transformTimestamp(e.rentTime[0]);
        this.rent.endtime=this.transformTimestamp(e.rentTime[1]);
        // 计算时间的差值
        let day=e.rentTime[1]-e.rentTime[0].getTime();
        // 计算总价格
        //判断计费规则，'0'表示以小时计租赁商品
        if(e.time){
          if (e.time==='0'){
            this.dayCount=''
            this.rent.dayPrice=''
            this.rent.singlePrice=this.good.rentPriceHour
            //时间戳获取小时数
            if (day % 86400000*24>0){
              let hourCount = Math.floor(day / 86400000*24+1) ;//如果超过一分钟按一小时计算
              this.yatolPrice=hourCount*this.good.rentPriceHour*e.number;//总价=小时数*小时价格*数量
              this.rent.tolRentPrice=this.yatolPrice+this.good.yaPrice;//总价=租赁价格+押金
              this.hourCount=hourCount;
              console.log('总的价格为'+this.rent.tolRentPrice+'总小时为'+hourCount)
            }else{
              let hourCount = Math.floor(day / 86400000*24) ;//未超过计算
              this.yatolPrice=hourCount*this.good.rentPriceHour*this.rent.number;
              this.rent.tolRentPrice=this.yatolPrice+this.good.yaPrice;
              this.hourCount=hourCount;
              console.log('总的价格为'+this.rent.tolRentPrice+'总小时为'+hourCount)
            }
          }else{
            this.hourCount=''
            this.rent.singlePrice=''
            this.rent.dayPrice=this.good.rentPriceDay
            //时间戳获取天数
            if (day % 86400000>0) {
              let dayCount = Math.floor(day / 86400000+1);//如果超过一分钟按一天计算
              this.yatolPrice=dayCount*this.good.rentPriceDay*e.number;
              this.rent.tolRentPrice=this.yatolPrice+this.good.yaPrice;
              this.dayCount=dayCount;
              console.log('总的价格为'+this.rent.tolRentPrice+'总天数为'+dayCount)
            }else{
              let dayCount = Math.floor(day / 86400000);//未超过计算
              this.yatolPrice=dayCount*this.good.rentPriceDay*e.number;
              this.rent.tolRentPrice=this.yatolPrice+this.good.yaPrice;
              this.dayCount=dayCount;
              console.log('总的价格为'+this.rent.tolRentPrice+'总天数为'+dayCount)
            }
          }
        }
      }
    },
    methods:{
      toStore(){
        // 传递参数到StoreGood页面
        this.$router.push({
          path: '/storeGood',
          name: 'StoreGood',
          params: {
            name: this.good.userId,
          }
        })
      },
      // 查询商铺
      initStore(){
        this.storeInfo=''
        this.getRequest('/good/data/oneStore/'+this.good.userId).then(resp=>{
          if (resp.cashStatus){
            this.storeInfo='的店铺'
          }else {
            console.log('商铺'+resp)
          }
        })
      },
      // 查询默认地址
     initDefaultAddress(){
       this.getRequest('/rent/address/?userId='+this.user.userId+'&&adrsDefault='+true).then(resp=>{
         if (resp){
           console.log('订单的地址是'+resp.addressId)
           this.rent.orderAddress=resp.addressId
         }
       })
     },
      // 添加订单
      addRent(){

        if (this.rentTime){
            var aData = new Date();
            this.rent.rentTime=this.transformTimestamp(aData);
            console.log(this.rent)
            // 添加
            this.postRequest('/rent/data/addRent',this.rent).then(resp=>{
              if (resp){
                console.log(resp.obj)
                this.$notify.success({ title: '成功', message: '添加成功' });
                this.dialogVisible = false
                // 判断是立即购买还是加入购物车
                if (this.ispay){
                  this.$router.push({
                    path: '/checkout',
                    name: 'CheckOut',
                    params: {
                      name: this.good.goodId,
                      rentName: resp.obj,
                    }
                  })
                }
              }
            })
        }else {
          this.$notify.warning({ title: '警告', message: '请输入租赁时间段' });
        }
      },

      // 根据商品id查询商品评价
      initComment(){
        // 取到路由带过来的参数
        let routerParams = this.$route.params.name;
        this.getRequest('/system/cfg/allComment/?goodId='+routerParams).then(resp => {
          if (resp.length<=0) {
            this.kong=true
          }else{
            this.kapian = true
            this.comment = resp;
            console.log(this.comment)
          }
        })
      },

      // 根据商品id查询商品
      initDetails(){
        // 取到main页面的路由带过来的参数
        let routerParams = this.$route.params.name;
        console.log('我过来了'+routerParams);
        this.postRequest('/goodList/selectById/?goodId='+routerParams).then(resp => {
          if (resp) {
            console.log(resp);
            this.good=resp;
            this.rent.myUserId=resp.userId;
            this.rent.goodId=resp.goodId;
            this.rentPriceDay=resp.rentPriceDay;
            this.rentPriceHour=resp.rentPriceHour;
            this.rent.yarent=resp.yaPrice;
            console.log('rent='+this.rent.goodId)
            this.initUsername()
            this.initDefaultAddress();
            this.initStore();
          }
        })
      },
      // 查询出租商品的用户
      initUsername(){
        this.postRequest('/personal/data/userById/'+this.good.userId).then(resp => {
          if (resp) {
            console.log(resp);
            this.username = resp;
          }
        })
      },

      addCheckout(pay){
        if(this.user) {
          // 验证表单
          this.ispay = pay
          this.dialogVisible = true
        }else {
          this.$router.push('/');
          this.$notify.info({ title: '消息', message: '尚未登录，请登录'});
        }
      },
      // 跳转到checkout页面，传商品的id过去
      checkout(pay){
        if(this.user) {
          this.ispay=pay
          this.dialogVisible=true
        }else {
          this.$router.push('/');
          this.$notify.info({ title: '消息', message: '尚未登录，请登录'});
        }

      },
    },
    components: {
      Main_footer,
      Main_header
    },
  }
</script>
<style lang="scss" scoped>
  @import "../../assets/mixin";

  .text {
    font-size: 14px;
  }

  .item {
    padding: 5px 0;
  }

  .box-card {
    width: 100%;
  }

  .store-content {
    clear: both;
    width: 1220px;
    min-height: 600px;
    padding: 0 0 25px;
    margin: 0 auto;
  }

  .gray-box {
    display: flex;
    padding: 60px;
    margin: 20px 0;
    .gallery-wrapper {
      .gallery {
        display: flex;
        width: 540px;
        .thumbnail {
          li:first-child {
            margin-top: 0px;
          }
          li {
            @include wh(80px);
            margin-top: 10px;
            padding: 12px;
            border: 1px solid #f0f0f0;
            border: 1px solid rgba(0, 0, 0, .06);
            border-radius: 5px;
            cursor: pointer;
            &.on {
              padding: 10px;
              border: 3px solid #ccc;
              border: 3px solid rgba(0, 0, 0, .2);
            }
            img {
              display: block;
              @include wh(100%);
            }
          }
        }
        .thumb {
          .big {
            margin-left: 20px;
          }
          img {
            display: block;
            @include wh(440px)
          }
        }
      }
    }
    // 右边
    .banner {
      width: 450px;
      h4 {
        font-size: 24px;
        line-height: 1.25;
        color: #000;
        margin-bottom: 13px;
      }
      h6 {
        font-size: 14px;
        line-height: 1.5;
        color: #bdbdbd;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .sku-custom-title {
        overflow: hidden;
        padding: 0 8px 18px 10px;
        position: relative;
      }
      .params-name {
        padding-right: 20px;
        font-size: 14px;
        color: #8d8d8d;
        line-height: 36px;
      }
      .num {
        padding: 29px 0 8px 10px;
        border-top: 1px solid #ebebeb;
        display: flex;
        align-items: center;
      }
      .buy {
        position: relative;
        border-top: 1px solid #ebebeb;
        padding: 30px 0 0 10px;
      }
    }
  }

  .item-info {

    .gray-box {
      padding: 0;
      display: block;
    }
    .img-item {
      width: 1220px;
      // padding: 1vw;
      text-align: center;
      img {
        width: 100%;
        height: auto;
        display: block;
      }
    }
  }

  .no-info {
    padding: 200px 0;
    text-align: center;
    font-size: 30px;
  }

  .price {
    display: block;
    color: #d44d44;
    font-weight: 700;
    font-size: 16px;
    line-height: 20px;
    text-align: right;
    i {
      padding-left: 2px;
      font-size: 24px;
    }
  }

  .minPrice {
    display: block;
    color: #484c49;
    font-weight: 700;
    font-size: 12px;
    line-height: 16px;
    text-align: right;
    i {
      padding-left: 2px;
      font-size: 14px;
    }
  }
  //评论样式
  .main {
    width: 1000px;
    margin: 0 auto;
    .title{
      color: #8a9aa9;
      font-size: 16px;
      font-weight: 400;
      text-align: center;
      padding: 1.67rem 0 5px;
    }
    .comment-form{
      display: flex;
      position: relative;
      padding: 10px;
      background-color: #fafbfc;
      border-radius: 3px;
      margin: 1.333rem 0;

      .avatar-box{
        flex: 0 0 auto;
        .header-img{
          margin-right: 10px;
        }
      }
      .form-box{
        flex: 1 1 auto;
        position: relative;
        .input-box{
          position: relative;
          display: inline-block;
          width: 100%;
          .reply-input {
            min-height: 20px;
            line-height: 22px;
            padding: 10px 110px 10px 10px;
            color: #ccc;
            background-color: #fff;
            border: 1px solid #f1f1f1;
            border-radius: 3px;
            text-align: left;

            &:empty:before {
              content: attr(placeholder);
            }

            &:focus:before {
              content: none;
            }
            &:focus {
              border: 1px solid #007fff;
              outline: none;
            }
          }
        }
        .action-box{
          display: flex;
          align-items: center;
          margin: .65rem 0 0;
          .emoji{
            position: relative;
            .emoji-box {
              display: flex;
              align-items: center;
              position: relative;
              color: #027fff;
              cursor: pointer;
              font-size: 14px;
              span{
                display:inline-block;
                padding-left: 20px;
                height: 18px;
                background: url('../../assets/img/login_head.gif')  left center no-repeat;
                background-size:18px;
              }

            }
          }
          .reply-btn-box {
            flex: 0 0 auto;
            margin-left: auto;
          }
        }

      }
    }

    .item {
      margin-bottom: 15px;
      padding: 10px;
      .comment {
        display: flex;

        .user-popover-box {
          height: 40px;
        }

        .content-box {
          border-bottom: 1px solid #f1f1f1;
          margin-left: 15px;
          flex: 1 1 auto;
          text-align: left;

          .meta-box {
            display: flex;
            align-items: center;
            font-size: 14px;
            line-height: 1.2;
            white-space: nowrap;
          }
          .content {
            margin-top: 5px;
            font-size: 16px;
            line-height: 1.8;
            word-wrap: break-word;
            white-space: pre-wrap;
            word-break: break-all;
            color: #505050;
            a{
              display:inline-block;
              margin:0 4px;
              font-size: 14px;
              font-weight: 400;
              color: #406599;
              cursor:pointer;
            }
          }
          .reply-stat {
            display: flex;
            margin-top: 10px;
            font-weight: 400;
            .author-time {
              font-size: 14px;
              color: #8a9aa9;
              cursor: default;
            }

            .icon-btn {
              flex: 0 0 auto;
              display: flex;
              justify-content: space-between;
              margin-left: auto;
              min-width: 8.8rem;
              color: #8a93a0;
              user-select: none;

              span {
                display: inline-block;
                margin: 0 8px;
                min-width: 100px;
                padding: 0 3px;
                cursor: pointer;
              }
            }
          }

          .reply-box {
            margin: 15px 0;
            background-color: #efefef;
            border-radius: 3px;
            .item{
              padding: 10px 0 10px 10px;
              &:not(:last-child) {
                border-bottom: 1px solid rgba(178,186,194,0.3);
              }
            }
          }
        }
      }
    }
  }

  .my-reply {
    padding: 10px;
    background-color: #fafbfc;

    .header-img {
      display: inline-block;
      vertical-align: top;
    }
    .reply-info {
      position: relative;
      display: inline-block;
      margin-left: 5px;
      width: 90%;
      @media screen and (max-width: 1200px) {
        width: 80%;
      }

      .reply-input {
        min-height: 20px;
        line-height: 22px;
        padding: 10px 110px 10px 10px;
        color: #ccc;
        background-color: #fff;
        border: 1px solid #f1f1f1;
        border-radius: 3px;
        text-align: left;
        &:empty:before {
          content: attr(placeholder);
        }

        &:focus:before {
          content: none;
        }

        &:focus {
          border: 1px solid #007fff;
          outline: none;
        }
      }
    }

    .reply-btn-box {
      .reply-btn {
        position: relative;
        float: right;
        height: 100%;
      }
    }
  }

  .my-comment-reply {
    margin-left: 50px;

    .reply-input {
      display: flex;
    }
  }

  .author-title:not(:last-child) {
    border-bottom: 1px solid rgba(178, 186, 194, 0.3);
  }

  .author-title {
    padding: 10px;

    .header-img {
      display: inline-block;
      vertical-align: top;
    }

    .author-info {
      display: inline-block;
      margin-left: 5px;
      width: 60%;
      height: 40px;
      line-height: 20px;

      >span {
        display: block;
        cursor: pointer;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      .author-name {
        color: #000;
        font-size: 18px;
        font-weight: bold;
      }

      .author-time {
        font-size: 14px;
      }
    }

    .icon-btn {
      width: 30%;
      padding: 0 !important;
      float: right;

      @media screen and (max-width: 1200px) {
        width: 20%;
        padding: 7px;
      }

      >span {
        cursor: pointer;
      }

      .iconfont {
        margin: 0 5px;
      }
    }

    .talk-box {
      margin: 0 50px;

      >p {
        margin: 0;
        text-align: left;
      }

      .reply {
        font-size: 16px;
        color: #000;
      }
    }

    .reply-box {
      margin: 10px 0 0 50px;
      background-color: #efefef;
    }
  }
</style>
